package com.liujit.upms.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.enums.SqlKeyword;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liujit.common.constants.ColumnConstant;
import com.liujit.common.constants.GlobalConstant;
import com.liujit.common.domain.params.BaseParams;
import com.liujit.common.domain.result.PageBean;
import com.liujit.common.exception.BusinessException;
import com.liujit.common.exception.DataNotFoundException;
import com.liujit.common.utils.DozerConvertor;
import com.liujit.common.utils.Pagination;
import com.liujit.upms.domain.model.SysDict;
import com.liujit.upms.domain.model.SysDictItem;
import com.liujit.upms.domain.params.SysDictParams;
import com.liujit.upms.domain.query.SysDictQuery;
import com.liujit.upms.domain.result.SysDictResult;
import com.liujit.upms.mapper.SysDictMapper;
import com.liujit.upms.service.SysDictItemService;
import com.liujit.upms.service.SysDictService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description 系统字典Service实现类
 * @Author liujun
 * @Date 2021-07-02 14:51:52
 */
@RequiredArgsConstructor
@Service
public class SysDictServiceImpl extends ServiceImpl<SysDictMapper, SysDict> implements SysDictService {

    private final SysDictItemService sysDictItemService;

    @Override
    public PageBean<SysDictResult> getPage(SysDictQuery query) {
        // 排序
        if (StrUtil.isNotBlank(query.getSortName()) && StrUtil.isNotBlank(query.getSortOrder())) {
            query.setSortName(ColumnConstant.CREATE_TIME);
            query.setSortOrder(SqlKeyword.DESC.getSqlSegment());
        }
        Page<SysDict> page = Pagination.page(query);
        LambdaQueryWrapper<SysDict> wrapper = new LambdaQueryWrapper();
        if (StrUtil.isNotBlank(query.getName())) {
            wrapper.like(SysDict::getName, query.getName());
        }
        if (StrUtil.isNotBlank(query.getCode())) {
            wrapper.like(SysDict::getCode, query.getCode());
        }
        if (query.getEnabled() != null) {
            wrapper.eq(SysDict::getEnabled, query.getEnabled());
        }
        if (query.getStartTime() != null) {
            wrapper.ge(SysDict::getCreateTime, query.getStartTime());
        }
        if (query.getEndTime() != null) {
            wrapper.le(SysDict::getCreateTime, query.getEndTime());
        }
        IPage<SysDict> iPage = super.page(page, wrapper);
        return PageBean.page(iPage, SysDictResult.class);
    }

    @Override
    public Boolean save(SysDictParams params) {
        SysDict entity = DozerConvertor.convertor(params, SysDict.class);
        // 添加
        if (params.getId() == null) {
            entity.setEnabled(GlobalConstant.ENABLED_YES);
            return super.save(entity);
        } else {
            SysDict sysDict = super.getById(params.getId());
            if (sysDict == null) {
                throw new DataNotFoundException();
            }
            return super.updateById(entity);
        }
    }

    @Override
    public SysDictResult get(Long id) {
        SysDict sysDict = super.getById(id);
        if (sysDict == null) {
            throw new DataNotFoundException();
        }
        return DozerConvertor.convertor(sysDict, SysDictResult.class);
    }

    @Override
    public SysDictResult getDetail(Long id) {
        SysDictResult result = this.get(id);
        result.setItems(sysDictItemService.getDictItem(result.getCode()));
        return result;
    }

    @Override
    public Boolean del(BaseParams params) {
        SysDict sysDict = super.getById(params.getId());
        if (sysDict == null) {
            throw new DataNotFoundException();
        }
        LambdaQueryWrapper<SysDictItem> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysDictItem::getDictCode, sysDict.getCode());
        Integer count = sysDictItemService.count(wrapper);
        if (count > 0) {
            throw new BusinessException("字典下有数据，不能删除");
        }
        return super.removeById(params.getId());
    }

    @Override
    public Boolean enabled(BaseParams params) {
        SysDict sysDict = super.getById(params.getId());
        if (sysDict == null) {
            throw new DataNotFoundException();
        }
        LambdaQueryWrapper<SysDictItem> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysDictItem::getDictCode, sysDict.getCode());
        Integer count = sysDictItemService.count(wrapper);
        if (count > 0) {
            throw new BusinessException("字典下有数据，不能禁用");
        }
        return super.updateById(new SysDict() {{
            setId(params.getId());
            setEnabled(GlobalConstant.ENABLED_YES);
        }});
    }

    @Override
    public Boolean disabled(BaseParams params) {
        SysDict sysDict = super.getById(params.getId());
        if (sysDict == null) {
            throw new DataNotFoundException();
        }
        return super.updateById(new SysDict() {{
            setId(params.getId());
            setEnabled(GlobalConstant.ENABLED_NO);
        }});
    }

    @Override
    public Integer getCountByName(Long id, String name) {
        LambdaQueryWrapper<SysDict> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysDict::getName, name);
        if (id != null) {
            wrapper.ne(SysDict::getId, id);
        }
        return super.count(wrapper);
    }

    @Override
    public Integer getCountByCode(Long id, String code) {
        LambdaQueryWrapper<SysDict> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysDict::getCode, code);
        if (id != null) {
            wrapper.ne(SysDict::getId, id);
        }
        return super.count(wrapper);
    }

    @Override
    public List<SysDictResult> getList() {
        LambdaQueryWrapper<SysDict> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysDict::getEnabled, GlobalConstant.ENABLED_YES);
        List<SysDict> list = super.list(wrapper);
        return DozerConvertor.convertor(list, SysDictResult.class);
    }
}
